package ca.jerg.data;

import java.io.*;
import java.util.*;

public class ErgPoint implements IErgPoint
{
	public static void main( String args[] ) throws IOException
	{
		System.out.println("Hello");
		ErgPoint ep = new ErgPoint(0.0,100);
		
		ep.write();
		
		BufferedWriter bw = new BufferedWriter(new FileWriter("test.erg"));
		ep.write(bw);
		bw.close();
		
		ErgPoint ep1 = new ErgPoint();
		
		ep1.write();
		BufferedReader br = new BufferedReader(new FileReader("test1.erg"));
		if ( ep1.read(br) != 0 )
			System.out.println("Read error");
		else
			ep1.write();
		br.close();
	}
	
	private double time;
	private int watts;
	
	/**
	 * Create a blank data point.  The data point
	 * is initialized to an invalid entry: (-1.0,-1).
	 */
	public ErgPoint()
	{
		time = -1.0;
		watts = -1;
	}

	/**
	 * Create an initialized data point.
	 * 
	 * @param time		the time index for this data point
	 * @param watts		the wattage level for this data point
	 */
	public ErgPoint( double time, int watts )
	{
		this.time = time;
		this.watts = watts;
	}

	/**
	 * Returns the data point's time index.
	 */
	public double getTime()
	{
		return time;
	}

	/**
	 * Returns the data point's wattage level.
	 */
	public int getWatts()
	{
		return watts;
	}
	
	//@Override
	public int read( BufferedReader in )
	{
		try
		{
			String s = in.readLine();
			if ( s.startsWith("[") && s.endsWith("]") )
				return 2;
			
			StringTokenizer st = new StringTokenizer(s," \t");
			try
			{
				time = Double.parseDouble(st.nextToken());
				watts = Integer.parseInt(st.nextToken());
			}
			catch ( NoSuchElementException nsee )
			{
				return 1;
			}
			catch ( NumberFormatException nfe )
			{
				return 1;
			}
		}
		catch ( IOException ioe )
		{
			return 1;
		}
		catch ( NullPointerException npe )
		{
			return 2;			
		}
		
		return 0;
	}
	
	/**
	 * Update the time index for this data point.  Care must
	 * be taken when updating the time index as the master list
	 * of data points must be kept in sequential time order for
	 * the generated output file to work correctly.  It is the
	 * responsibility of the caller to maintain the integrity 
	 * of master list of data points.
	 * 
	 * @param time		the new time index for this data point
	 */
	public void setTime( double time )
	{
		this.time = time;
	}

	/**
	 * Update the wattage level for this data point.
	 * 
	 * @param watts		the new wattage level for this data point
	 */
	public void setWatts( int watts )
	{
		this.watts = watts;
	}

	//@Override
	public void write()
	{
		System.out.print(String.valueOf(time));
		System.out.print("\t");
		System.out.println(String.valueOf(watts));		
	}

	//@Override
	public boolean write( BufferedWriter out )
	{
		try
		{
			out.write(String.valueOf(time));
			out.write("\t");
			out.write(String.valueOf(watts));
		}
		catch ( IOException ioe )
		{
			return false;
		}

		return true;
	}	
}
